<template>
  <div class="app-container">
    <el-card class="box-card search-div"v-show="showSearch">
      <el-form :inline="true" :model="listQuery" ref="queryForm">
        <el-form-item label="岗位名称">
          <el-input placeholder="请输入岗位名称" v-model="listQuery.postName" clearable/>
        </el-form-item>
        <el-form-item label="部门" style="width:20%;white-space: nowrap;">
          <treeselect :append-to-body='true' style="width:80%;" v-model="listQuery.deptId" :multiple="false" :options="deptData" :load-options="loadOptions" :default-expand-level="2" placeholder="请选择部门"/>
        </el-form-item>
        <el-button type="primary" @click="handleSearch" icon="el-icon-search">搜索</el-button>
        <el-button @click="handleResetQuery" icon="el-icon-refresh">重置</el-button>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" v-hasPermi="['sys_post_add']" class="el-icon-plus" @click="add">新增</el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </div>
      <el-table :data="list" v-loading.body="listLoading" border fit highlight-current-row class="table-list">
        <el-table-column label="序号" type="index" min-width="5%"/>
        <el-table-column align="center" label="岗位编码" prop="code">
        </el-table-column>
        <el-table-column align="center" label="岗位名称" prop="postName">
        </el-table-column>
        <el-table-column align="center" label="部门" prop="deptName">
        </el-table-column>
        <el-table-column align="center" label="修改时间" prop="updateTime" width="150px"></el-table-column>
        <el-table-column align="center" label="操作" width="160" fixed="right">
          <template slot-scope="scope">
            <el-button plain v-hasPermi="['sys_post_edit']" type="violet" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button plain v-hasPermi="['sys_post_remove']" type="danger" @click="removeByIds(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
     <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </el-card>
        <!--弹出框-->
        <el-dialog :title="dialogTitle" :width="'600px'" :visible.sync="showDialog" :close-on-click-modal="false">
          <el-form :rules="rules" ref="saveSysPostData" :model="saveSysPostData" label-width="90px">
            <el-form-item label="部门:" prop="deptId">
              <treeselect v-model="saveSysPostData.deptId" :multiple="false" :options="deptData" :load-options="loadOptions" :default-expand-level="2" placeholder="请选择部门"/>
            </el-form-item>
            <el-form-item label="岗位编码:" prop="code">
              <el-input v-model="saveSysPostData.code" :maxlength="20"  placeholder="最多输入20个字符"></el-input>
            </el-form-item>
            <el-form-item label="岗位名称:" prop="postName">
              <el-input v-model="saveSysPostData.postName" :maxlength="20"  placeholder="最多输入20个字符"></el-input>
            </el-form-item>
            <el-form-item label="岗位描述:" prop="remark">
              <el-input type="textarea" rows="3" v-model="saveSysPostData.remark" :maxlength="200"  placeholder="最多输入200个字符"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="showDialog = false">取 消</el-button>
            <el-button type="primary" @click="onSave('saveSysPostData')">确 定</el-button>
          </div>
        </el-dialog>
  </div>
</template>

<script>
import * as sysPost from './js/post'
import * as sysDept from '../dept/js/dept'
import { mapGetters } from 'vuex'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'postList',
  components:{Treeselect},
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      showSearch:true,
      listQuery: {
        page: 1, limit: 10, postName: ''
      },
      showDialog: false,
      saveSysPostData: {
        id: '', code: '', postName: '', remark: '',deptId: null,deptIds:''
      },
      rules: {
        postName: [{ required: true, message: '请输入岗位名称', trigger: 'blur' }],
        deptId: [{ required: true, message: '请选择部门', trigger: 'blur' }]
      },
      dialogTitle: '添加岗位',
      deptData: []
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  created() {
    this.getList()
    this.getDeptTree()
  },
  methods: {
    loadOptions() {},
    getList() { // 获取所有岗位
      this.listLoading = true
      sysPost.fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
     // 部门树
    getDeptTree() {
      sysDept.getDeptTreeList().then((response) => {
        this.deptData = response.data
      })
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleResetQuery(){
      this.resetForm("queryForm")
      this.handleSearch()
    },
    //删除
    removeByIds(id) {
      this.$confirm('请确定是否删除该岗位?', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        sysPost.removeByIds(id).then((response) => {
          this.$message({ type: 'success', message: '操作成功!' })
          this.getList()
        })
      }).catch(function(error) { console.log(error) })
    },
    // 新增
    add(pit, index) {
      this.showDialog = true
      this.dialogTitle = '添加岗位'
      // 清空表单数据
      this.$nextTick(() => {
        this.resetForm('saveSysPostData')
        this.saveSysPostData.id = ''
      })
    },
    // 编辑
    handleEdit(index, row) {
      this.showDialog = true
      this.dialogTitle = '编辑岗位'
      this.$nextTick(() => {
        this.resetForm('saveSysPostData')
        this.saveSysPostData.id = row.id
        this.saveSysPostData.code = row.code
        this.saveSysPostData.postName = row.postName
        this.saveSysPostData.remark = row.remark
        this.saveSysPostData.deptId = row.deptId
      })
    },
    // 重置表单 解决验证提示的问题
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 保存
    onSave(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.showDialog = false
          sysPost.save(this.saveSysPostData).then((response) => {
            this.$message({ type: 'success', message: '操作成功!' })
            this.getList()
          }).catch(function(error) {
            console.log(error)
          })
        }
      })
    }
  }
}
</script>
